package managedbean.CRM.SaleOrder; 
 
import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB; 
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.CRM.SaleOrderBeanLocal;
 
@ManagedBean(name="viewCustomersSaleOrdersManagerBean")
@ViewScoped 
public class ViewCustomersSaleOrdersManagerBean implements Serializable{ 
    @EJB 
    private SaleOrderBeanLocal saleOrderBeanLocal; 
    
    private Customer selectedCustomer;
    private SaleOrder selectedSaleOrder;
    private List<SaleOrder> saleOrders;
    private String statusMessage;
    
    public ViewCustomersSaleOrdersManagerBean() { 
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    
    public void initView()
    {
        if(selectedCustomer==null){
                selectedCustomer = (Customer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedCustomer");           
        this.setSaleOrders(selectedCustomer.getSaleOrders());
    }
    }
        
    public void makeCashInAdvance(ActionEvent event) throws IOException
    {
        selectedSaleOrder=this.getSelectedSaleOrder();
        if (selectedSaleOrder !=null)
        {
            if (selectedSaleOrder.getCashInAdvance()==null)
            {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleOrder", selectedSaleOrder);
                FacesContext.getCurrentInstance().getExternalContext().redirect("../cashInAdvance/addCashInAdvance.xhtml");                
            }
            else
            {
                System.out.println("jump here");
                statusMessage="This Sale Order has Cash In Advance before. Please choose the option modify.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result:"+statusMessage  , ""));         
            }
        }        
    }
    
    public void modifyCashInAdvance(ActionEvent event) throws IOException
    {
        if (selectedSaleOrder !=null)
        {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleOrder", selectedSaleOrder);
                FacesContext.getCurrentInstance().getExternalContext().redirect("../cashInAdvance/editCashInAdvance.xhtml");                
        }
     }

    public List<SaleOrder> getSaleOrders() {
        return saleOrders;
    }

    public void setSaleOrders(List<SaleOrder> saleOrders) {
        this.saleOrders = saleOrders;
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public SaleOrder getSelectedSaleOrder() {
        return selectedSaleOrder;
    }

    public void setSelectedSaleOrder(SaleOrder selectedSaleOrder) {
        this.selectedSaleOrder = selectedSaleOrder;
    }
}
    
